//
//  EventDatetimePickerViewController.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/4/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "EventDatetimePickerViewController.h"

@implementation EventDatetimePickerViewController

@synthesize eventTimestampPicker;
@synthesize eventTimestampConfirmButton;
@synthesize delegate;
@synthesize dateFormatter=_dateFormatter;
@synthesize eventDateTimeSelect=_eventDateTimeSelect;
@synthesize displayEventTimestamp;
@synthesize dateComponents=_dateComponents;
@synthesize calendar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem = self.eventTimestampConfirmButton;
    self.displayEventTimestamp.layer.cornerRadius = 10;
    self.displayEventTimestamp.layer.borderWidth = 1.0;
    self.displayEventTimestamp.layer.borderColor = [[UIColor grayColor] CGColor];
    
    self.displayEventTimestamp.text = [self.dateFormatter stringFromDate:[calendar dateFromComponents:self.dateComponents]];
    
    self.eventTimestampPicker.date = [calendar dateFromComponents:self.dateComponents];
    
    self.title = @"Date & Time";
}

- (void)viewDidUnload
{
    [self setEventTimestampPicker:nil];
    [self setEventTimestampConfirmButton:nil];
    [self setDateFormatter:nil];
    [self setEventDateTimeSelect:nil];
    [self setDisplayEventTimestamp:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)eventTimestampPickerValueChanged:(id)sender 
{
    self.displayEventTimestamp.text = [self.dateFormatter stringFromDate:self.eventTimestampPicker.date];

}

- (IBAction)eventTimestampConfirmed:(id)sender 
{
    [self.delegate setEventTimestamp:self.eventTimestampPicker.date];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.delegate = nil;
    [eventTimestampPicker release];
    [eventTimestampConfirmButton release];
    [dateFormatter release];
    [_eventDateTimeSelect release];
    [displayEventTimestamp release];
    [super dealloc];
}

@end
